package two.chapter_1.c1_2;

import java.util.concurrent.atomic.AtomicInteger;

public class Counter {

    private String name;
    private AtomicInteger count;
    public Counter(String name){
        this.name=name;
        count=new AtomicInteger();
    }

    /**
     * 计数器加1
     */
    public void increment(){
        count.incrementAndGet();
    }

    /**
     * 返回计数器
     * @return
     */
    public int tally(){
        return count.get();
    }

    @Override
    public String toString() {
        return name+"'s tally is "+tally();
    }
}
